package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryExecuteProgressReq;
import com.sg.dto.base.app.req.ImplementHoverExecuteProgressReq;
import com.sg.dto.base.app.req.QueryExecuteProgressDetailReq;
import com.sg.dto.base.app.req.QueryExecuteProgressListReq;
import com.sg.entity.OmsExecuteProgress;
import com.sg.service.base.app.MOmsExecuteProgressService;
import com.wicket.okrapp.common.annotations.BanAuto;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsExecuteProgressMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsExecuteProgressServiceImpl
        extends ServiceImpl<OmsExecuteProgressMapper, OmsExecuteProgress>
        implements MOmsExecuteProgressService {

    @Autowired
    OmsExecuteProgressMapper omsExecuteProgressMapper;

    /**
     * 3-15-01新增执行进度[6958]
     * gen by moon at 5/29/2023, 4:16:52 PM
     */
    @Trace(operationName = "3-15-01新增执行进度")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addExecuteProgress(OmsExecuteProgress omsExecuteProgress) {
        omsExecuteProgress.setExecuteProgressId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsExecuteProgress.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsExecuteProgress.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsExecuteProgress.setIsValid("FALSE");
        if (super.save(omsExecuteProgress)) {
            return omsExecuteProgress.getExecuteProgressId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-15-01查执行进度详情[6959]
     * gen by moon at 5/27/2023, 12:43:36 AM
     */
    @Trace(operationName = "3-15-01查执行进度详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsExecuteProgress queryExecuteProgressDetail(QueryExecuteProgressDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<OmsExecuteProgress> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsExecuteProgress> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsExecuteProgress retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-15-01修改执行进度[6960]
     * gen by moon at 5/27/2023, 12:43:36 AM
     */
    @Trace(operationName = "3-15-01修改执行进度")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateExecuteProgress(OmsExecuteProgress omsExecuteProgress) {
        omsExecuteProgress.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsExecuteProgress.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsExecuteProgress);
    }

    /**
     * 3-15-01删除执行进度[6961]
     * gen by moon at 5/27/2023, 12:43:37 AM
     */
    @Trace(operationName = "3-15-01删除执行进度")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteExecuteProgress(String executeProgressId) {

        return super.removeById(executeProgressId);
    }

    /**
     * 3-15-01批量查执行进度[6962]
     * gen by moon at 5/27/2023, 12:43:41 AM
     */
    @Trace(operationName = "3-15-01批量查执行进度")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsExecuteProgress> batchQueryExecuteProgress(BatchQueryExecuteProgressReq reqDto) {

        try {
            QueryWrapper<OmsExecuteProgress> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            if (reqDto.getProgressRecordList() == null || reqDto.getProgressRecordList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("object_id", reqDto.getProgressRecordList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsExecuteProgress.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsExecuteProgress.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsExecuteProgress.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsExecuteProgress>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-15-01执行悬停执行进度[6964]
     * gen by moon at 5/27/2023, 12:43:44 AM
     */
    @Trace(operationName = "3-15-01执行悬停执行进度")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    @BanAuto
    public OmsExecuteProgress implementHoverExecuteProgress(ImplementHoverExecuteProgressReq reqDto) {
        try {
            long count = 2000;
            while (true) {
                if (count == 0) {
                    break;
                }
                reqDto.setIsValid("FALSE");
                QueryWrapper<OmsExecuteProgress> queryWrapper = new QueryWrapper<>();
                Map<String, Object> fieldInfoMap = null;
                fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
                CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
                queryWrapper.orderByDesc("create_time");
                if (super.list(queryWrapper) != null && super.list(queryWrapper).size() > 0) {
                    return super.list(queryWrapper).get(0);
                }
                count--;
                Thread.sleep(10);
            }
            throw new BizException("-1", false);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }

    }

    /**
     * 3-15-01批量删执行进度[6981]
     * gen by moon at 5/28/2023, 6:21:55 PM
     */
    @Trace(operationName = "3-15-01批量删执行进度")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteExecuteProgress(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M3-15-01查询执行进度测试列表[6983]
     * gen by moon at 5/28/2023, 6:21:56 PM
     */
    @Trace(operationName = "M3-15-01查询执行进度测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsExecuteProgress> queryExecuteProgressTestList(OmsExecuteProgress omsExecuteProgress) {

        try {
            QueryWrapper<OmsExecuteProgress> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(omsExecuteProgress);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsExecuteProgress.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsExecuteProgress.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsExecuteProgress.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsExecuteProgress>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M3-15-01批量删执行进度测试[6984]
     * gen by moon at 5/28/2023, 6:21:59 PM
     */
    @Trace(operationName = "M3-15-01批量删执行进度测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteExecuteProgressTest(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-15-01修改执行进度无约束[7007]
     * gen by moon at 5/29/2023, 2:09:40 PM
     */
    @Trace(operationName = "3-15-01修改执行进度无约束")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateExecuteProgressUnrestraint(OmsExecuteProgress omsExecuteProgress) {
        omsExecuteProgress.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsExecuteProgress);
    }

    /**
     * 3-15-01更新执行进度值数据库加[7093]
     * gen by moon at 6/1/2023, 4:08:04 PM
     */
    @Trace(operationName = "3-15-01更新执行进度值数据库加")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    @BanAuto
    public boolean refreshExecuteProgressValueDatabaseAdd(OmsExecuteProgress omsExecuteProgress) {
        UpdateWrapper<OmsExecuteProgress> uw = new UpdateWrapper<>();
        uw.eq("execute_progress_id", omsExecuteProgress.getExecuteProgressId());
        uw.setSql("execute_progress_value = execute_progress_value + " + omsExecuteProgress.getExecuteProgressValue());//ADD_NUMERICAL
        omsExecuteProgress.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsExecuteProgress.setExecuteProgressValue(null);
        return super.update(omsExecuteProgress, uw);

    }

    /**
     * 3-15-01查执行进度列表[7160]
     * gen by moon at 6/8/2023, 1:32:57 AM
     */
    @Trace(operationName = "3-15-01查执行进度列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsExecuteProgress> queryExecuteProgressList(QueryExecuteProgressListReq reqDto) {

        try {
            QueryWrapper<OmsExecuteProgress> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsExecuteProgress.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsExecuteProgress.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsExecuteProgress.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsExecuteProgress>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }

    }
}
